package at.chl.lution.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.crypto.spec.IvParameterSpec;

public class AccuracySortedList extends ArrayList<Long>{
	
	private Map<Long,Long> valueMap;

	public AccuracySortedList(Map<Long, List<Long>> accuracy) {
		
		valueMap=new HashMap<Long, Long>();
		for(Entry<Long, List<Long>> entry:accuracy.entrySet()){
			Long average=calcAverage(entry.getValue());
			if(average==0){
				System.out.println();
			}
			addSortedId(entry.getKey(),average);
		}
		
		
	}

	public void addSortedId(Long input, Long value) {
		
		if(this.size()==0){
			valueMap.put(input, value);
			this.add(input);
			return;
		}
		
		int index=0;
		
		for(Long entry:this){
			
			Long entryValue=valueMap.get(entry);
			
			if(entryValue>value){
				
				break;
				
			}
			index++;
		}
		valueMap.put(input, value);
		this.add(index, input);
		
	}

	private Long calcAverage(List<Long> values) {
		long average=0;
		boolean isAllZero=true;
		
		for(Long value:values){
			average+=value;
			if(isAllZero){
				isAllZero=(value==0);
			}
		}
		average=average/values.size();
		
		//check if there are really all values zero
		//if not set average to 1
		if(average==0&&isAllZero==false){
			average=1;
		}
		
		return average;
	}

	public List<Long> getPerfects() {
		List<Long> perfects=new ArrayList<Long>();
		for(Long id:this){
			if(valueMap.get(id)==0){
				perfects.add(id);
			}
		}
		return perfects;
	}

	public Long getValue(Long id) {
		return valueMap.get(id);
	}
	
}
